package com.wl.wuliu.controller;

import com.wl.wuliu.core.controller.BaseController;
import com.wl.wuliu.core.page.TableDataInfo;
import com.wl.wuliu.entity.SysUser;
import com.wl.wuliu.entity.Truck;
import com.wl.wuliu.entity.QueryEntity;
import com.wl.wuliu.result.AjaxResult;
import com.wl.wuliu.service.SysUserService;
import com.wl.wuliu.service.TruckService;
import com.wl.wuliu.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @Author: gutaozhen
 * @Date: 2024/5/15
 */
@RestController
@RequestMapping("/truck")
public class TuckController extends BaseController {

    @Autowired
    private TruckService truckService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 列表查询
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo findList(QueryEntity queryEntity) {
        //如果不是联络员，查询全部
        if (!StringUtils.isEmpty(queryEntity.getUserId())) {
            SysUser sysUser = sysUserService.findSysUser(new SysUser(queryEntity.getUserId()));
            if (sysUser != null && !"lianluo".equals(sysUser.getRoleKey())) {
                queryEntity.setUserId(null);
            }
        }
        startPage();
        List<Truck> list = truckService.findList(queryEntity);

        return getDataTable(list);
    }

    /**
     * 保存运输车辆信息
     * @param truck
     * @return
     */
    @PostMapping("/save")
    public AjaxResult saveTruck(@RequestBody Truck truck) {

        if (StringUtils.isEmpty(truck.getDriverId())) {
            return AjaxResult.error("司机不能为空。");
        }

        if (StringUtils.isEmpty(truck.getGoods())) {
            return AjaxResult.error("货物名称不能为空。");
        }
        if (StringUtils.isEmpty(truck.getInstallLocation())) {
            return AjaxResult.error("装货地点不能为空。");
        }
        if (StringUtils.isEmpty(truck.getUninstallLocation())) {
            return AjaxResult.error("卸货地点不能为空。");
        }
        if (StringUtils.isEmpty(truck.getUserId())) {
            return AjaxResult.error("联络员不能为空。");
        }

        return truckService.saveTruck(truck);
    }

    /**
     * 删除运输车俩信息
     * @return
     */
    @GetMapping("/delete")
    public AjaxResult deleteTruck(@RequestParam String id) {

        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("非法参数");
        }

        return truckService.deleteTruck(id);
    }

    /**
     * 修改车辆信息
     * @param truck
     * @return
     */
    @PostMapping("/update")
    public AjaxResult updateTruck(@RequestBody Truck truck) {

        return truckService.updateTruck(truck);
    }

    /**
     * 根据id查询车辆信息
     * @param id
     * @return
     */
    @GetMapping("/getTruck")
    public AjaxResult getTruck(@RequestParam String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("非法参数");
        }

        Truck truck = truckService.getTruck(id);

        return AjaxResult.success(truck);
    }

    /**
     * 确认发车
     * @param truck
     * @return
     */
    @GetMapping("/startCar")
    public AjaxResult startCar(Truck truck) {

        return truckService.updateTruckStatus(truck);
    }

    /**
     * 确认卸货/确认异常
     * @param truck
     * @return
     */
    @GetMapping("/stopCar")
    public AjaxResult stopCar(Truck truck) {

        return truckService.updateTruckStatus(truck);
    }

    /**
     * 车辆异常
     * @param truck
     * @return
     */
    @GetMapping("/errorCar")
    public AjaxResult errorCar(Truck truck) {

        return truckService.updateTruckStatus(truck);
    }

    /**
     * 处理异常
     * @param truck
     * @return
     */
    @GetMapping("/handleErrorCar")
    public AjaxResult handleErrorCar(Truck truck) {

        return truckService.updateTruckStatus(truck);
    }

    /**
     * 统计发车数量
     * @return
     */
    @GetMapping("/statisticsCarNumber")
    public AjaxResult statisticsCarNumber(String startTime,String endTime) {

        return AjaxResult.success(truckService.statisticsCarNumber(startTime,endTime));
    }

    /**
     * 导入运输车信息
     * @param file
     * @throws IOException
     */
    @PostMapping("/importTruck")
    public void importTruck(MultipartFile file) {

        truckService.importTruck(file);
    }

}
